package com.ruoyi.community.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.BaseNoticeSection;
import com.ruoyi.community.domain.SimpleNoticeContent;
import com.ruoyi.community.service.IBaseNoticeSectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.community.mapper.BaseNoticeMapper;
import com.ruoyi.community.domain.BaseNotice;
import com.ruoyi.community.service.IBaseNoticeService;

/**
 * 通知公告Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-16
 */
@Service
public class BaseNoticeServiceImpl implements IBaseNoticeService 
{
    @Autowired
    private BaseNoticeMapper baseNoticeMapper;

    @Autowired
    private IBaseNoticeSectionService baseNoticeSectionService;

    /**
     * 查询通知公告
     * 
     * @param noticeId 通知公告ID
     * @return 通知公告
     */
    @Override
    public BaseNotice selectBaseNoticeById(String noticeId)
    {
        BaseNotice resultNotice = baseNoticeMapper.selectBaseNoticeById(noticeId);
        Integer viewNumber = resultNotice.getViewNumber() + 1;
        resultNotice.setViewNumber(viewNumber);
        updateBaseNotice(resultNotice);
        return baseNoticeMapper.selectBaseNoticeById(noticeId);
    }

    /**
     * 查询通知公告列表
     * 
     * @param baseNotice 通知公告
     * @return 通知公告
     */
    @Override
    public List<BaseNotice> selectBaseNoticeList(BaseNotice baseNotice)
    {
        baseNotice.setCommunityId(SecurityUtils.getCommunityId());
        return baseNoticeMapper.selectBaseNoticeList(baseNotice);
    }

//    // 返回”社区公告“模块各板块的公告，不包括社区党建和业委会公告
//    @Override
//    public List<BaseNotice> selectBaseNoticeListInNoticeModule(BaseNotice baseNotice)
//    {
//        List<BaseNoticeSection> sectionList = baseNoticeSectionService.selectBaseNoticeSectionListUserDefined()
//    }

    @Override
    public List<BaseNotice> selectBaseNoticePreviewWithoutPic(BaseNotice baseNotice)
    {
        baseNotice.setCommunityId(SecurityUtils.getCommunityId());
        return baseNoticeMapper.selectBaseNoticePreviewWithoutPic(baseNotice);
    }

    @Override
    public List<BaseNotice> selectBaseNoticePreviewWithPic(BaseNotice baseNotice)
    {
        baseNotice.setCommunityId(SecurityUtils.getCommunityId());
        List<BaseNotice> list = baseNoticeMapper.selectBaseNoticeList(baseNotice);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setContent(null);
        }
        return list;
    }

    @Override
    public List<BaseNotice> selectCommunityNoticePreviewWithoutPic(BaseNotice baseNotice)
    {
        baseNotice.setCommunityId(SecurityUtils.getCommunityId());
        return baseNoticeMapper.selectCommunityNoticePreviewWithoutPic(baseNotice);
    }

    @Override
    public List<BaseNotice> selectCommunityNoticePreviewWithPic(BaseNotice baseNotice)
    {
        baseNotice.setCommunityId(SecurityUtils.getCommunityId());
        return baseNoticeMapper.selectCommunityNoticePreviewWithPic(baseNotice);
    }

    /**
     * 新增通知公告
     * 
     * @param baseNotice 通知公告
     * @return 结果
     */
    @Override
    public int insertBaseNotice(BaseNotice baseNotice)
    {
        baseNotice.setAuthor(SecurityUtils.getUsername());
        baseNotice.setCommunityId(SecurityUtils.getCommunityId());
        Date date = DateUtils.getNowDate();
        baseNotice.setSyncTime(date);
        baseNotice.setViewNumber(0);
        // 若富文本内容中包含图片，则提取链接以供缩略图使用
        String content = baseNotice.getContent();
        int pic_start = content.indexOf("<img");
        if (pic_start == -1){
            baseNotice.setPicUrl(null);
        }else {
            int pic_end = content.indexOf("\">", pic_start);
            String pic_url = content.substring(pic_start+10, pic_end);
            baseNotice.setPicUrl(pic_url);
        }
        return baseNoticeMapper.insertBaseNotice(baseNotice);
    }

    /**
     * 修改通知公告
     * 
     * @param baseNotice 通知公告
     * @return 结果
     */
    @Override
    public int updateBaseNotice(BaseNotice baseNotice)
    {
        return baseNoticeMapper.updateBaseNotice(baseNotice);
    }

    /**
     * 批量删除通知公告
     * 
     * @param noticeIds 需要删除的通知公告ID
     * @return 结果
     */
    @Override
    public int deleteBaseNoticeByIds(String[] noticeIds)
    {
        return baseNoticeMapper.deleteBaseNoticeByIds(noticeIds);
    }

}
